<!-- 资源位编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改资源位':'添加资源位'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    :before-close="confirmClose"
    @update:visible="updateVisible"
    :close-on-click-modal="false">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-form-item
        label="资源位名称:"
        prop="title">
        <el-input
          :maxlength="255"
          v-model="form.title"
          placeholder="请输入资源位名称"
          clearable/>
      </el-form-item>

      <el-form-item
        label="资源位icon:"
        prop="icon">
        <uploadImage :limit="1" v-model="form.icon">
        </uploadImage>
      </el-form-item>
      <el-form-item
        label="选中时icon:"
        prop="icon">
        <uploadImage :limit="1" v-model="form.bg_icon">
        </uploadImage>
      </el-form-item>

      <!-- <el-form-item
        label="组件名称:"
        prop="compoent">
        <el-input
          :maxlength="255"
          v-model="form.compoent"
          placeholder="请输入组件名称"
          clearable/>
      </el-form-item> -->
      <el-form-item
        label="版本号:"
        prop="version">
        <el-input
          :maxlength="255"
          v-model="form.version"
          placeholder="请输入版本号(如1.20)"
          clearable/>
      </el-form-item>

      <el-form-item label="资源位权重:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="权重越大排序越靠前"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>

      <el-form-item
        label="链接:"
        prop="link">
        <el-input
          :maxlength="255"
          v-model="form.link"
          placeholder="请输入链接"
          clearable/>
      </el-form-item>

      <el-form-item label="是否开启" prop="status">
        <el-radio-group
          v-model="form.status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="2">否</el-radio>
        </el-radio-group>
      </el-form-item>


      <el-form-item label="备注:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          v-model="form.notes"
          placeholder="请输入备注"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import UploadImage from "@/components/uploadImage.vue";

export default {
  name: 'BoxResourceTabEdit',
  components: {UploadImage},
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  // components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status: 1}, this.data),
      // 表单验证规则
      rules: {
        title: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        icon: [
          {required: true, message: '请输入icon', trigger: 'blur'}
        ],
        bg_icon: [
          {required: true, message: '请输入选中icon', trigger: 'blur'}
        ],
        version: [
          {required: true, message: '请输入版本号', trigger: 'blur'}
        ],
        link: [
          {required: true, message: '请输入链接', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '请输入状态，1、启用，0、禁用', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序', trigger: 'blur'}
        ],
        notes: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      bannerOptions: [],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        if(this.form.version !== undefined){
          this.form.version = this.$convertNumberToVersion(this.form.version);
        }
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  created() {
    this.initBannerData()
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.form.version = parseInt(
            this.form.version.split(".").join(""),
            10
          );
          this.$http.post('/boxResourceTab/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },

    confirmClose() {
      this.$confirm('确定要返回吗？', '温馨提示').then(() => {
        this.updateVisible(false)
        if (!this.isUpdate) {
          this.form = {};
        }
      }).catch(() => {

      });
    },
  }
}
</script>

<style scoped>
</style>
